<?php

// +----------------------------------------------------------------------
// | 系统路径
// +----------------------------------------------------------------------

define('URL', 'http://lfzb.yokey.top/');

// +----------------------------------------------------------------------
// | 公共常量
// +----------------------------------------------------------------------

define('COMMON_ENABLE', '1');//公共：开启
define('COMMON_DISABLE', '2');//公共：关闭

define('PAGE_NUMBER', '100');//分页数量


// +----------------------------------------------------------------------
// | 输入输出
// +----------------------------------------------------------------------

function get_param($name = '')
{
    if (empty($name)) return input('get.json');
    return input('get.' . $name);
}

function post_param($name = '')
{
    if (empty($name)) return input('post.json');
    return input('post.' . $name);
}

function output_success()
{
    header('content-type:application/json');
    //拼接数据
    $newData = array();
    $newData['code'] = COMMON_ENABLE;
    $newData['msg'] = 'success';
    $newData['data'] = COMMON_ENABLE;
    //输出数据
    echo json_encode($newData);
    die;
}

function output_data($data)
{
    header('content-type:application/json');
    //拼接数据
    $newData = array();
    $newData['code'] = COMMON_ENABLE;
    $newData['msg'] = 'success';
    $newData['data'] = $data;
    //输出数据
    echo json_encode($newData);
    die;
}

function output_error_non()
{
    header('content-type:application/json');
    //拼接数据
    $newData = array();
    $newData['code'] = COMMON_DISABLE;
    $newData['msg'] = '数据不存在~';
    $newData['data'] = '';
    //输出数据
    echo json_encode($newData);
    die;
}

function output_error_param()
{
    header('content-type:application/json');
    //拼接数据
    $newData = array();
    $newData['code'] = COMMON_DISABLE;
    $newData['msg'] = '参数错误~';
    $newData['data'] = '';
    //输出数据
    echo json_encode($newData);
    die;
}

function output_error_power()
{
    header('content-type:application/json');
    //拼接数据
    $newData = array();
    $newData['code'] = COMMON_DISABLE;
    $newData['msg'] = '权限不足~';
    $newData['data'] = '';
    //输出数据
    echo json_encode($newData);
    die;
}

function output_error($error)
{
    header('content-type:application/json');
    //拼接数据
    $newData = array();
    $newData['code'] = COMMON_DISABLE;
    $newData['msg'] = $error;
    $newData['data'] = '';
    //输出数据
    echo json_encode($newData);
    die;
}

function output_error_exist($tag)
{
    header('content-type:application/json');
    //拼接数据
    $newData = array();
    $newData['code'] = COMMON_DISABLE;
    $newData['msg'] = $tag . '已存在~';
    $newData['data'] = '';
    //输出数据
    echo json_encode($newData);
    die;
}

function output_data_table($data, $count)
{
    header('content-type:application/json');
    //拼接数据
    $newData = array();
    $newData['code'] = '0';
    $newData['msg'] = '';
    $newData['count'] = $count;
    $newData['data'] = $data;
    //输出数据
    echo json_encode($newData);
    die;
}

// +----------------------------------------------------------------------
// | 时间日期
// +----------------------------------------------------------------------

function format_time($time)
{
    if (empty($time)) {
        return null;
    }
    return date('Y年m月d日 H:i:s', $time);
}

function format_date($time)
{
    if (empty($time)) {
        return null;
    }
    return date('Y年m月d日', $time);
}

function handler_time($time)
{
    $time = (int)substr($time, 0, 10);
    $int = time() - $time;
    if ($int <= 2) {
        $str = sprintf('刚刚');
    } elseif ($int < 60) {
        $str = sprintf('%d秒前', $int);
    } elseif ($int < 3600) {
        $str = sprintf('%d分钟前', floor($int / 60));
    } elseif ($int < 86400) {
        $str = sprintf('%d小时前', floor($int / 3600));
    } elseif ($int < 2592000) {
        $str = sprintf('%d天前', floor($int / 86400));
    } else {
        $str = date('Y-m-d H:i:s', $time);
    }
    return $str;
}

// +----------------------------------------------------------------------
// | 常用功能
// +----------------------------------------------------------------------

function md5_encrypt($salt, $content)
{
    //先加密一次MD5
    $password = '';
    $oneArray = array();
    $oneStr = md5($salt . $content);
    for ($i = 0; $i < strlen($oneStr); $i++) {
        $oneArray[] = substr($oneStr, $i, 1);
    }
    //在加密一次MD5
    $twoArray = array();
    $twoStr = md5($salt . $content);
    for ($i = 0; $i < strlen($twoStr); $i++) {
        $twoArray[] = substr($twoStr, $i, 1);
    }
    //拼接成新的字符串
    for ($i = 0; $i < strlen($oneStr); $i++) {
        $password = $password . $oneArray[$i] . $twoArray[$i];
    }
    return md5($password);
}

function redirection($url)
{
    header('Location: ' . $url);
    die();
}

function get_cookie($name)
{
    if (empty($_COOKIE[$name])) return null;
    return $_COOKIE[$name];
}

function set_cookie($name, $value = '')
{
    if (empty($value)) {
        setcookie($name, '', time() - 360000, '/');
        return;
    }
    setcookie($name, $value, time() + 360000, '/');
}
